package com.yao.component.circle;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hzl.PictureDialog;
import com.squareup.picasso.Picasso;
import com.yao.component.pictureselector.CommonUtil;
import com.yao.component.pictureselector.PictureDelete;
import com.yao.component.pictureselector.PictureSelector;
import com.yao.sdk.net.NetConstant;
import com.yao.sdk.util.Utils;
import com.yzf.PublishActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Circle Photo 列表的数据适配器
 */
public class PhotoListAdapter2 extends RecyclerView.Adapter<PhotoListAdapter2.ViewHolder> {



    private ArrayList<String> imageList = new ArrayList<>();


    public void setData(ArrayList<String> imageList) {
        if (Utils.isEmpty(imageList)) {
            return;
        }
        this.imageList = imageList;
    }

    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        public ImageView circlePhoto;

        public ViewHolder(View v) {
            super(v);
            circlePhoto = (ImageView)v.findViewById(R.id.circle_photo);
        }
    }


    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public PhotoListAdapter2.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                           int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_circle_photo2, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        //final Post circleItem = imageList.get(position);
//        final String imageUri = NetConstant.BASE_IMAGE_URL + imageList.get(position);
        if (imageList.get(position).equals("add_picture")){
            Picasso.with(holder.circlePhoto.getContext()).load(R.drawable.add_big).resize(150, 150)
                    .centerCrop().into(holder.circlePhoto);
        }else {
            Picasso.with(holder.circlePhoto.getContext()).load("file://"+imageList.get(position)).resize(150, 150)
                    .centerCrop().into(holder.circlePhoto);
        }


        holder.circlePhoto.setBackground(null);
        holder.circlePhoto.setPadding(5,5,5,5);
        holder.circlePhoto.setLayoutParams(new RelativeLayout.LayoutParams(
                ( CommonUtil.getDisplayWidth(holder.circlePhoto.getContext())-20)/4, ( CommonUtil.getDisplayWidth(holder.circlePhoto.getContext())-20)/4));
        holder.circlePhoto.setTag(position);
        holder.circlePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (Integer) v.getTag();
                if (imageList.get(position).equals("add_picture")){
                    PictureDialog pay = new PictureDialog(holder.circlePhoto.getContext(), new PictureDialog.onPictureDialogListener() {
                        @Override
                        public void onCamare() {

                        }

                        @Override
                        public void onAlbum() {
                            PictureSelector pictureSelector = new PictureSelector(holder.circlePhoto.getContext(),9-imageList.size()+1, new PictureSelector.onPictureSelectListener() {
                                @Override
                                public void onSelect(List<String> selectPath) {
                                    imageList.remove("add_picture");
                                    imageList.addAll(selectPath);
                                    if (!(imageList.size()>=9)){
                                        imageList.add("add_picture");
                                    }
                                    PhotoListAdapter2.this.notifyDataSetChanged();
                                }

                                @Override
                                public void onCancel() {

                                }
                            });
                            pictureSelector.show();
                        }

                        @Override
                        public void onCancle() {

                        }
                    });
                    pay.show();

                }else {
                    List<String> tmp=new ArrayList<String>();
                    tmp.addAll(imageList);

                    if (tmp.contains("add_picture")){
                        tmp.remove("add_picture");
                    }
                    PictureDelete delete=new PictureDelete(holder.circlePhoto.getContext(),tmp ,position, new PictureDelete.onPictureDeleteListener() {
                        @Override
                        public void onResult(List<String> selectPath) {
                            imageList.clear();
                            imageList.addAll(selectPath);
                            if (imageList.size()<9){
                                imageList.add("add_picture");
                            }
                            PhotoListAdapter2.this.notifyDataSetChanged();
                        }
                    });
                    delete.show();
                }

            }
        });
    }

    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return imageList.size();
    }


}


